//
//  RouteListCell.m
//  Cycling
//
//  Created by 迟国强 on 2023/4/5.
//

#import "RouteListCell.h"
#import "RouteDetailModel.h"
#import "UserModel.h"

#import "BaseRequestApi.h"

@implementation RouteListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    self.bgView.layer.cornerRadius = 16;
    self.bgView.layer.masksToBounds = YES;
    
    self.routeImageView.userInteractionEnabled = YES;
    
//    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(testSaveImageAction)];
//    tapGesture.numberOfTapsRequired = 1;
//    [self.routeImageView addGestureRecognizer:tapGesture];
}

- (void)testSaveImageAction {
    
    if(self.routeImageView.image) {
        UIImageWriteToSavedPhotosAlbum(self.routeImageView.image, nil, nil, nil);
    }
    
}

- (IBAction)clickedGoodBtn:(UIButton *)sender {
    if (self.goodBlock) {
        self.goodBlock(self.model);
    }
}

- (IBAction)clickedCollectionBtn:(UIButton *)sender {
    if (self.collectionBlock) {
        self.collectionBlock(self.model);
    }
}
- (IBAction)shareBtnAction:(UIButton *)sender {
    if (self.shareBlock) {
        self.shareBlock(self.model);
    }

}

- (void)setModel:(RouteDetailModel *)model {
    _model = model;
    NSString *imageString = model.fulllineimg;
    if(model.fullimgs.count){
        imageString = model.fullimgs.firstObject;
    } else if(model.imgs.count) {
        imageString = model.imgs.firstObject;
    }else {
        if(model.fulllineimg.length > 0){
            imageString = model.fulllineimg;
        } else if(model.lineimg.length > 0) {
            imageString = model.lineimg;
        }
    }
    if(![imageString hasPrefix:@"http"]) {
        imageString = [NSString stringWithFormat:@"%@%@", BaseUrl, imageString];
    }
    if(imageString.length == 0) {
        NSLog(@"哎，错误了，图片路径确实不存在");
    }
    [self.routeImageView sd_setImageWithURL:[NSURL URLWithString:imageString]];

    self.routeImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.routeImageView.clipsToBounds = YES;
//    self.routeNameL.text = model.name;
    self.distanceL.text = [NSString stringWithFormat:@"%.1fmiles", CSMeterToMileage(model.actualdistance.floatValue)];
    self.timeL.text = model.timelength;
    self.velocityL.text = [NSString stringWithFormat:@"%.1f %@",[model.climb doubleValue], CSFeetUnitString];
    self.kcalL.text = model.calories;
    self.addressL.text = model.endpost;
    self.collectionTitleLbl.text = model.collectionnum;
    self.goodTitleLbl.text = model.praisenum;
    self.collectionIconView.highlighted = model.iscollection;
    self.goodIconView.highlighted = model.ispraise;
    self.groundL.text = model.ground_text;
//    if (UserModel.currentUserInfo.user_id == model.userid.integerValue) {
//        self.hongqiImageV.hidden = NO;
//    } else {
//        self.hongqiImageV.hidden = YES;
//    }
    if (UserModel.currentUserInfo.user_id == model.userid.integerValue) {
        self.routeNameL.text = nil;
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[model.name stringByAppendingString:@" "]];
        NSTextAttachment *imageAttach = [NSTextAttachment new];
        imageAttach.image = [UIImage imageNamed:@"hongqi_gaoliang"];
        imageAttach.bounds = CGRectMake(0, -2, 18, 24);
        [attributeString appendAttributedString:[NSAttributedString attributedStringWithAttachment:imageAttach]];
        self.routeNameL.attributedText = attributeString;
    } else {
        self.routeNameL.attributedText = nil;
        self.routeNameL.text = model.name;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
}

@end
